package controllers

import (
	"encoding/json"
	"goravel/app/services"

	"github.com/goravel/framework/contracts/http"
)

// 结构体controller
type UserController struct {
	//Dependent services
}

func NewUserController() *UserController {
	return &UserController{
		//Inject services
	}
}

// 类中的方法
// (r *UserController)表示属于结构体 Usercontroller
func (r *UserController) Show(ctx http.Context) http.Response {

	userService := services.NewUserService()
	user, err := userService.Find()
	if err != nil {
		panic(err.Error())
	}

	// struct转json
	jsonData, err := json.Marshal(user)

	if err != nil {
		return ctx.Response().Json(http.StatusInternalServerError, http.Json{
			"error": err.Error(),
		})
	}
	return ctx.Response().String(http.StatusOK, string(jsonData))
}
